package com.yex.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yex.common.utils.DateUtil;
import com.yex.utils.security.AESOperator;
import com.yex.utils.vo.KlineVO;
import com.yex.utils.vo.TickerVO;
import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;



@Slf4j
public class AutoTradeUtils {
    public static BigDecimal getOkcoinPrice(String symbol, int count) {
        BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("http://api.zb.com/data/v1/ticker?market=" + symbol.toLowerCase());
            SocketAddress addr = new InetSocketAddress("47.75.188.253", Integer.parseInt("13780"));
            Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
            URLConnection conn = url1.openConnection(proxy);
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
            BufferedReader br = new BufferedReader(new InputStreamReader(url1.openStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class).get("ticker");
//	            Map ltc = ((Map) JSONObject.fromObject(sb.toString()).get("ticker"));
            last = MathUtil.getBigDecimal(new BigDecimal(ltc.get("last").toString()), count);
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;
    }

    public static BigDecimal getBinancePrice(String symbol, int count) {
        BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.binance.com/api/v3/ticker/price?symbol=" + symbol);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url1.openConnection();    
            httpUrlConn.setDoInput(true);    
            httpUrlConn.setRequestMethod("GET");    
            httpUrlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream()));
         
           
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = JSON.parseObject(sb.toString(), Map.class);
//	            Map ltc = ((Map) JSONObject.fromObject(sb.toString()));
            log.info("");
            last = MathUtil.getBigDecimal(new BigDecimal(ltc.get("price").toString()), count);
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;
    }

    
    public static BigDecimal getBiBoxPrice(String symbol, int count) {
        BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.bibox.com/v1/mdata?cmd=market&pair=" + symbol);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url1.openConnection();    
            httpUrlConn.setDoInput(true);    
            httpUrlConn.setRequestMethod("GET");    
            httpUrlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            JSONObject ltc = JSON.parseObject(sb.toString());
//	            Map ltc = ((Map) JSONObject.fromObject(sb.toString()));
            last = MathUtil.getBigDecimal(new BigDecimal(((JSONObject)ltc.get("result")).get("last").toString()), count);
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;
    }
    
    
    public static BigDecimal getKKCoinPrice(String symbol, int count) {
        BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.kkcoin.com/rest/allticker");

            HttpURLConnection httpUrlConn = (HttpURLConnection) url1.openConnection();    
            httpUrlConn.setDoInput(true);    
            httpUrlConn.setRequestMethod("GET");    
            
            httpUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
           
            BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            JSONArray ltc = JSONArray.parseArray(sb.toString());
            if(ltc != null) {
            	for(int i=0;i<ltc.size();i++) {
            		JSONArray json = (JSONArray)ltc.get(i);
            		if(symbol.equals(json.get(0))) {
            			last = MathUtil.getBigDecimal(new BigDecimal(json.get(7).toString()), count);
            		}
            	}
            }

        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;
    }
    
    

    public void test() {
        BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            StringBuffer buf = new StringBuffer();
            buf.append("symbol=");
            buf.append("LTCBTC");
            buf.append("&side=");
            buf.append("BUY");
            buf.append(" &type=LIMIT&timeInForce=GTC&quantity=");
            buf.append("1");
            buf.append("&price=");
            buf.append("0.1");
            buf.append("&recvWindow=5000&timestamp=");
            buf.append(new Date().getTime());


            String sign = AESOperator.hmacSHA256(buf.toString().getBytes(), "NyhjbH0DpmpYCEWISigS1gfd1N8caMuQQdOmh45INEMtE8sPKDddyUPXpEOUP28B".getBytes());
            buf.append("&signature=");
            buf.append(sign);

            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.binance.com/api/v3/order/test?" + buf.toString());

            BufferedReader br = new BufferedReader(new InputStreamReader(url1.openStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            System.out.println(tmp);

        } catch (Exception e) {
            e.printStackTrace();
        }
//		 AESOperator.hmacSHA256(data, key);
    }

    public static BigDecimal getHuobiPrice(String symbol, int count) {
        BigDecimal last = BigDecimal.ZERO;
        try {
            log.info(symbol.toUpperCase()+"====Huobi获取价格=={}", symbol);
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.huobi.com/market/detail?symbol=" + symbol.toLowerCase());
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class).get("tick");
//	            Map ltc = ((Map) JSONObject.fromObject(sb.toString()).get("tick"));
            last = MathUtil.getBigDecimal(new BigDecimal(ltc.get("close").toString()), count);
            log.info(symbol.toUpperCase()+"====Huobi获取价格=={}", last);
        } catch (Exception e) {
            log.error(symbol.toUpperCase()+"====Huobi获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;
    }

    public static BigDecimal getOkexPrice(String symbol, int count) {
        /*BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://www.okex.com/api/spot/v3/instruments/"+symbol.toUpperCase()+"/ticker" );
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setRequestProperty("contentType","application/x-www-form-urlencoded");
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class);
//	            Map ltc = ((Map) JSONObject.fromObject(sb.toString()).get("ticker"));
            last = getBigDecimal(new BigDecimal(ltc.get("last").toString()), count);

        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;*/
        BigDecimal last = BigDecimal.ZERO;
        try {
            log.info(symbol.toUpperCase()+"====okey获取价格=={}", symbol);
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
//            URL url1 = new URL("https://www.okcoin.com/api/v1/ticker.do?symbol=" + symbol);
            URL url1 = new URL("https://www.okex.com/api/spot/v3/instruments/"+symbol.toUpperCase()+"/ticker");
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setRequestProperty("contentType","application/json");
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class);
            last = MathUtil.getBigDecimal(new BigDecimal(String.valueOf(ltc.get("last"))), count);
            log.info(symbol.toUpperCase()+"====okex获取价格=={}", last);
        } catch (Exception e) {
            log.error(symbol.toUpperCase()+"====okex获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return last;
    }

    public static BigDecimal getBitZ(String symbol, int count) {
        BigDecimal last = BigDecimal.ZERO;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://apiv2.bitz.com/Market/ticker?symbol=" + symbol.toLowerCase());
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            log.info("bit-z数据：{}", sb);
            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class).get("data");
//	            Map ltc = ((Map) JSONObject.fromObject(sb.toString()).get("ticker"));
            last = MathUtil.getBigDecimal(new BigDecimal(ltc.get("now").toString()), count);
            log.info("bit-z价格：{}", last);
        } catch (Exception e) {
            log.error("获取bit-z注解出错：", e);
            e.printStackTrace();
        }
        return last;
    }

    

    public static List<KlineVO> getOkcoinKline(String symbol, BigDecimal exchange) {
        List<KlineVO> klineVOs = new ArrayList<>();
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
//            URL url1 = new URL("https://www.okcoin.com/api/v1/kline.do?type=1min&size=180&symbol=" + symbol);
            URL url1 = new URL("https://www.okex.com/api/spot/v3/instruments/"+symbol+"/candles?granularity=60");
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setRequestProperty("contentType","application/json");
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }

            List<HashMap> listList = JSON.parseArray(sb.toString(), HashMap.class);
            Long ts = System.currentTimeMillis();
            int t = 0;
            for (Map map : listList) {
                KlineVO klineVO = new KlineVO();
                
                klineVO.setPrice(MathUtil.getBigDecimal(MathUtil.mul(new BigDecimal(String.valueOf(map.get("close"))),exchange),2));
                klineVO.setTime(DateUtil.format(DateUtil.HOUR_MINUTE, ts - 60000 * t));
                klineVOs.add(klineVO);
                t++;
                
            }
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return klineVOs;
    }

    public static TickerVO getOkcoinTicker(String symbol) {
        TickerVO tickerVO = null;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
//            URL url1 = new URL("https://www.okcoin.com/api/v1/ticker.do?symbol=" + symbol);
            URL url1 = new URL("https://www.okex.com/api/spot/v3/instruments/"+symbol.toUpperCase()+"/ticker");
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setRequestProperty("contentType","application/json");
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }

            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class);
            tickerVO = new TickerVO();
            tickerVO.setHigh(new BigDecimal(String.valueOf(ltc.get("high_24h"))));
            tickerVO.setLow(new BigDecimal(String.valueOf(ltc.get("low_24h"))));
            tickerVO.setLast(new BigDecimal(String.valueOf(ltc.get("last"))));

        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }

        return tickerVO;
    }


    public static List<KlineVO> getBinanceKline(String symbol,BigDecimal exchange) {
        List<KlineVO> klineVOs = new ArrayList<>();
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.binance.com/api/v1/klines?interval=1m&limit=180&symbol=" + symbol);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }

            List<LinkedList> listList = JSON.parseArray(sb.toString(), LinkedList.class);

            for (List list : listList) {
                KlineVO klineVO = new KlineVO();
                klineVO.setPrice(MathUtil.getBigDecimal(MathUtil.mul(new BigDecimal(String.valueOf(list.get(4))),exchange),2));
                klineVO.setTime(DateUtil.format(DateUtil.HOUR_MINUTE, (Long) list.get(0)));
                klineVOs.add(klineVO);
            }
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return klineVOs;
    }

    public static TickerVO getBinanceTicker(String symbol) {
        TickerVO tickerVO =null;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.binance.com/api/v1/ticker/24hr?symbol=" + symbol);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }

            Map ltc = JSON.parseObject(sb.toString(), Map.class);
            tickerVO = new TickerVO();
            tickerVO.setHigh(new BigDecimal(String.valueOf(ltc.get("highPrice"))));
            tickerVO.setLow(new BigDecimal(String.valueOf(ltc.get("lowPrice"))));
            tickerVO.setLast(new BigDecimal(String.valueOf(ltc.get("lastPrice"))));

        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }

        return tickerVO;
    }


    public static List<KlineVO> getHuobiKline(String symbol,BigDecimal exchange) {
        List<KlineVO> klineVOs = new ArrayList<>();
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.huobi.com/market/history/kline?period=1min&size=180&symbol=" + symbol);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = JSON.parseObject(sb.toString(), Map.class);
            Long ts = (Long) ltc.get("ts");
            List<Map> listList = JSON.parseArray(ltc.get("data").toString(), Map.class);
            Integer t = 0;
            for (Map map : listList) {
                KlineVO klineVO = new KlineVO();
                klineVO.setPrice(MathUtil.getBigDecimal(MathUtil.mul(new BigDecimal(String.valueOf(map.get("close"))),exchange),2));
                klineVO.setTime(DateUtil.format(DateUtil.HOUR_MINUTE, ts - 60000 * t));
                klineVOs.add(klineVO);
                t++;
            }
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return klineVOs;
    }


    public static TickerVO getHuobiTicker(String symbol) {
        TickerVO tickerVO = null;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.huobi.com/market/detail/merged?symbol=" + symbol);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }

            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class).get("tick");
            tickerVO = new TickerVO();
            tickerVO.setHigh(new BigDecimal(String.valueOf(ltc.get("high"))));
            tickerVO.setLow(new BigDecimal(String.valueOf(ltc.get("low"))));
            tickerVO.setLast(new BigDecimal(String.valueOf(ltc.get("close"))));

        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }

        return tickerVO;
    }

    public static List<KlineVO> getYexKline(String symbol,BigDecimal exchange) {
        List<KlineVO> klineVOs = new ArrayList<>();
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://www.yex.com/api/v1?param=kline&step=60&symbol=" + symbol);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = JSON.parseObject(sb.toString(), Map.class);
            List<LinkedList> listList = JSON.parseArray(ltc.get("data").toString(), LinkedList.class);
            for (List list : listList) {
                KlineVO klineVO = new KlineVO();
                klineVO.setPrice(MathUtil.getBigDecimal(MathUtil.mul(new BigDecimal(String.valueOf(list.get(4))),exchange),2));
                klineVO.setTime(DateUtil.format(DateUtil.HOUR_MINUTE, (Long) list.get(0)));
                klineVOs.add(klineVO);
            }
            Collections.reverse(klineVOs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return klineVOs;
    }


    public static TickerVO getYexTicker(String symbol) {
        TickerVO tickerVO = null;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://www.yex.com/api/v1?param=market&symbol=" + symbol);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }

            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class).get("data");
            tickerVO = new TickerVO();
            tickerVO.setHigh(new BigDecimal(String.valueOf(ltc.get("high"))));
            tickerVO.setLow(new BigDecimal(String.valueOf(ltc.get("low"))));
            tickerVO.setLast(new BigDecimal(String.valueOf(ltc.get("last"))));

        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }

        return tickerVO;
    }

    public static List<Map<String,Object>> getCoinmarketcapListings() {
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.coinmarketcap.com/v2/listings/");
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = JSON.parseObject(sb.toString(), Map.class);
            List<Map> listList = JSON.parseArray(ltc.get("data").toString(), Map.class);
            List<Map<String,Object>> hashMapList=new ArrayList<>();
            for (Map map : listList) {
                Map<String,Object> hashMap = new HashMap<>(16);
                hashMap.put("id",map.get("id"));
                hashMap.put("name",map.get("symbol"));
                hashMapList.add(hashMap);
            }
        return hashMapList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static TickerVO getCoinmarketcapTicker(Integer id) {
        TickerVO tickerVO = null;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.coinmarketcap.com/v2/ticker/" + id);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = (Map) JSON.parseObject(sb.toString(), Map.class).get("data");
            ltc= (Map) ltc.get("quotes");
            ltc= (Map) ltc.get("USD");
            tickerVO = new TickerVO();
            tickerVO.setLast(new BigDecimal(String.valueOf(ltc.get("price"))));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tickerVO;
    }

    public static TickerVO getBitfinexTicker(String symbol) {
        TickerVO tickerVO = null;
        try {
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URL url1 = new URL("https://api.bitfinex.com/v1/pubticker/" + symbol.toUpperCase());
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            Map ltc = JSON.parseObject(sb.toString(), Map.class);
            tickerVO = new TickerVO();
            tickerVO.setHigh(new BigDecimal(String.valueOf(ltc.get("high"))));
            tickerVO.setLow(new BigDecimal(String.valueOf(ltc.get("low"))));
            tickerVO.setLast(new BigDecimal(String.valueOf(ltc.get("last_price"))));
        } catch (Exception e) {
        	log.error("获取价格出错=={}", symbol);
            e.printStackTrace();
        }
        return tickerVO;
    }

    private static HostnameVerifier hv = new HostnameVerifier() {
        @Override
        public boolean verify(String urlHostName, SSLSession session) {
            System.out.println("Warning: URL Host: " + urlHostName + " vs. "
                    + session.getPeerHost());
            return true;
        }
    };


    public static void main(String[] args) {
    	System.out.println("kk-coin="+getKKCoinPrice("MTC_ETH", 8));
//    	System.out.println("bibox==>"+getBiBoxPrice("EOS_ETH", 2));
//    	System.out.println("getBitZ==>"+getBitZ("EOS_ETH", 2));

    }

    public static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext
                .getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc
                .getSocketFactory());
    }

    public static class miTM implements TrustManager,
            X509TrustManager {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(
                X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(
                X509Certificate[] certs) {
            return true;
        }

        @Override
        public void checkServerTrusted(
                X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }

        @Override
        public void checkClientTrusted(
                X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }


    }
}
